package com.example.redistest.controller;

import com.alibaba.fastjson2.JSON;
import com.example.redistest.domain.Student;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.apache.tomcat.util.json.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName MyController
 * @Description
 * @Author huyingliang
 * @Date 2024/1/4 15:11
 */
@RequestMapping("/my")
@RestController
public class RedisController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/test")
    public String test() {
        Student student = new Student("hyl", 18, "asdasd", 999);
        stringRedisTemplate.opsForValue().set("student", JSON.toJSONString(student));

        String s = stringRedisTemplate.opsForValue().get("student");
        Student student1 = JSON.parseObject(s, Student.class);
        return student1.toString();
    }


    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }


    @GetMapping("/add")
    public String add() {
        stringRedisTemplate.opsForValue().set("name", "hyl");
        return "success";
    }

    @GetMapping("/increase")
    public String increase() {

        Long age = stringRedisTemplate.opsForValue().increment("age", -1);

        if (stringRedisTemplate.opsForValue().increment("age", -1) >= 0) {
            return "success";
        }
        return "fail";
    }

}
